package com.bbzn.device.client.service.emq.data;

import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.dataobject.PhotoUpload;
import com.bbzn.device.client.service.EmqStrategy;
import com.bbzn.device.client.service.PhotoUploadService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;

/**
 * @Author: wulongbo
 * @Date : 2021/5/7 11:05
 * 拍照上传
 */
@Slf4j
@Component
public class EmqPhotoUploadStrategy implements EmqStrategy {
    @Autowired
    PhotoUploadService photoUploadService;

    @Override
    public String emqAction(String payLoad) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(payLoad);
            String imei = jsonObject.getString("imei");
            if (StringUtils.isEmpty(imei)) {
                return "json解析错误";
            }
            String url = jsonObject.getString("info");
            if (StringUtils.isEmpty(url)) {
                return "json解析错误";
            }
            String address = jsonObject.getString("address");
            if (StringUtils.isEmpty(url)) {
                return "json解析错误";
            }
            PhotoUpload photoUpload = new PhotoUpload();
            photoUpload.setImei(imei);
            photoUpload.setUrl(url);
            photoUpload.setAddress(address);
            photoUpload.setCreateTime(new Timestamp(System.currentTimeMillis()));
            photoUpload.setCreatedBy("emq");
            photoUploadService.insertSelective(photoUpload);
        } catch (Exception e) {
            return "拍照上传失败！";
        }
        return "拍照上传成功！";
    }

}
